<?php

namespace app\controllers;

use app\models\Slide;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\Message;
use app\models\Desk;



class FrontController extends Controller
{
    public function actionLogin()
    {
    }
    /*显示留言 json*/
    public function actionShowMsg()
    {
        $request=Yii::$app->request;
        $msg_md=new Message();
        $slide_md=new Slide();
        $slide_list=$slide_md->getlistAll();
        $s_list=array();
        foreach ($slide_list as $temp)
        {
            $s_list[$temp['name']]=$temp['url'];
        }
        $desk_md=new Desk();
        #轮播图片 地址列表 获取
        $slide=array(
            'http://liuyan.ijiechen.com'.$s_list['slide_1'],
            'http://liuyan.ijiechen.com'.$s_list['slide_2'],
            'http://liuyan.ijiechen.com'.$s_list['slide_3'],
        );
        #留言信息获取
        $desk_code=$request->get('desk_code');
        if(is_null($desk_code) OR $desk_code=='')
        {
            $re_json=array(
                'status'=>'success',
                'content'=>'暂无留言内容',
                'slide'=>$slide,
            );
            return json_encode($re_json);
        }
        #获取 通过审核 未删除 留言
        $msg_info=$msg_md->getOneByDeskCode($desk_code);
        if(!$msg_info)
        {
            $re_json=array(
                'status'=>'success',
                'content'=>'暂无留言内容',
                'slide'=>$slide,
            );
            return json_encode($re_json);
        }
        $re_json=array(
            'status'=>'success',
            'content'=>$msg_info['message'],
            'slide'=>$slide,
        );
        return json_encode($re_json);
    }
    /*添加留言 json*/
    public function actionAddMsg()
    {
        $request=Yii::$app->request;
        $msg_md=new Message();
        $desk_md=new Desk();
        $nick_name=$request->get('username');
        $head_img=$request->get('useravatar');
        $desk_code=$request->get('desk_code');
        $message=$request->get('liuyancontent');
        #表单验证
        if(is_null($nick_name) OR is_null($head_img) OR is_null($desk_code) OR is_null($message) OR $nick_name=='' OR $head_img=='' OR $desk_code=='' OR $message=='')
        {
            $re_json=array(
                'status'=>'error',
            );
            return json_encode($re_json);
        }
        #获取桌子信息
        $desk_info=$desk_md->getOneByCode($desk_code);
        if(!$desk_info)
        {
            $re_json=array(
                'status'=>'error',
            );
            return json_encode($re_json);
        }
        #添加数据
        if($msg_md->createNew($desk_info['id'],$nick_name,$head_img,$message))
        {
            $re_json=array(
                'status'=>'success'
            );
            return json_encode($re_json);
        }
        else
        {
            $re_json=array(
                'status'=>'error',
            );
            return json_encode($re_json);
        }

    }

    public function actionTest()
    {
        $desk_md=new Desk();
        $msg_md=new Message();
        $code='0116df5cd5427b36497fe61b73225d98';
//        $re=$desk_md->getOneByCode($code);
        $re=$msg_md->getOneByDeskCode($code);
        $rte='aaa';

    }

}
